﻿using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using MyIDP.Models;
using MyIDP.Permission;

namespace MyIDP
{
    public class IdpDbContext : IdentityDbContext<ApplicationUser>
    {
        public IdpDbContext(DbContextOptions<IdpDbContext> opt) : base(opt)
        {

        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<ApplicationUser>().ToTable("ApplicationUsers");
            #region #
            //builder.Entity<IdentityUserLogin<string>>().ToTable("ApplicationLogins");
            //builder.Entity<IdentityUserClaim<string>>().ToTable("ApplicationUserClaims");
            //builder.Entity<ApplicationUserRole>().ToTable("ApplicationUserRoles");
            //builder.Entity<IdentityUserToken<string>>().ToTable("ApplicationUserTokens");
            //builder.Entity<ApplicationRole>().ToTable("ApplicationRoles");
            //builder.Entity<IdentityRoleClaim<string>>().ToTable("ApplicationRoleClaims");
            //builder.Entity<ApplicationUserRole>().HasKey(t => t.Id).HasName("PK_UserRole_ID_KEY");
            #endregion
            MyPasswordHasher ph = new MyPasswordHasher();
            builder.Entity<ApplicationUser>().HasData(
                new ApplicationUser()
                {
                    Id = Guid.NewGuid().ToString(),
                    RealName = "alice1",
                    UserName = "alice1",
                    PasswordHash = ph.HashPassword(null, "123456")
                });

            #region 初始化用戶与角色的种子数据
            //1. 更新用戶与角色的外鍵
            builder.Entity<ApplicationUser>(
                u => u.HasMany(x => x.UserRoles).WithOne().HasForeignKey(ur => ur.UserId).IsRequired()
                );
            //2. 添加管理员角色
            var adminRoleId = "f8df1775-e889-46f4-acdd-421ec8d9ba64";
            var testRoleId = "4e9ae737-0105-49b5-8a0f-08173b01d77a";
            builder.Entity<IdentityRole>().HasData(
                new IdentityRole()
                {
                    Id = adminRoleId,
                    Name = "Admin",
                    NormalizedName = "Admin".ToUpper()
                },

                new IdentityRole()
                {
                    Id = testRoleId,
                    Name = "Test",
                    NormalizedName = "test".ToUpper()
                }
            );
            //3. 添加用户
            var adminUserId = "f8df1775-e889-46f4-acdd-421ec8d9ba65";
            var testUserId = "4e9ae737-0105-49b5-8a0f-08173b01d77b";
            ApplicationUser adminUser = new ApplicationUser
            {
                Id = adminUserId,
                UserName = "admin",
                NormalizedUserName= "admin".ToUpper(),
                RealName = "admin",
                NormalizedEmail = "admin@qq.com".ToUpper(),
                Email = "admin@qq.com",
                TwoFactorEnabled = false,
                EmailConfirmed = true,
                PhoneNumber = "123456789",
                PhoneNumberConfirmed = false,

            };
            adminUser.PasswordHash = ph.HashPassword(adminUser, "123456");
            ApplicationUser testUser = new ApplicationUser
            {
                Id = testUserId,
                UserName = "test",
                NormalizedUserName = "test".ToUpper(),
                RealName = "test",
                NormalizedEmail = "test@qq.com".ToUpper(),
                Email = "test@qq.com",
                TwoFactorEnabled = false,
                EmailConfirmed = true,
                PhoneNumber = "123456789",
                PhoneNumberConfirmed = false,
                PasswordHash = ph.HashPassword(adminUser, "123456")
            };
            builder.Entity<ApplicationUser>().HasData(new List<ApplicationUser> { adminUser, testUser });

            //4. 给用户加入管理员角色
            builder.Entity<IdentityUserRole<string>>().HasData(new List<IdentityUserRole<string>>() {
                    new IdentityUserRole<string>()
                    {
                        RoleId = adminRoleId,
                        UserId = adminUserId
                    },
                    new IdentityUserRole<string>()
                    {
                        RoleId = testRoleId,
                        UserId = testUserId
                    }
                }
            );
            #endregion

        }
    }
}
